package com.laikan.reader.bookfriend.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import com.laikan.reader.R;
import com.laikan.reader.bookfriend.base.CommonAdapter;
import com.laikan.reader.bookfriend.holder.FooterHolder;
import com.laikan.reader.bookfriend.holder.LViewHolder;

import java.util.List;

/**
 * Created by lingyt on 2016/11/15.
 * 支持加载更多的adapter
 */
public abstract class BaseListAdapter<T> extends CommonAdapter<T>{

    public static final int ITEM_TYPE_NORMAL = 0;
    public static final int ITEM_TYPE_FOOTER = 1;
    private FooterHolder mFooterHolder;


    public BaseListAdapter(Context context, int layoutId){
        this(context,layoutId,null);
    }

    public BaseListAdapter(Context context, int layoutId, List<T> datas) {
        super(context, layoutId, datas);
    }

    @Override
    public int getItemCount() {
        if (null == mDatas){
            return 0;
        }else{
            return mDatas.size()+1;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == getItemCount()-1){
            return ITEM_TYPE_FOOTER;
        }
        return ITEM_TYPE_NORMAL;
    }

    @Override
    public LViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE_FOOTER){
            mFooterHolder = getMoreHolder();
            return mFooterHolder;
        }
        return super.onCreateViewHolder(parent, viewType);
    }
    @Override
    public void onBindViewHolder(LViewHolder holder, int position) {
        if (ITEM_TYPE_FOOTER == getItemViewType(position)){
            if (mFooterHolder != null) {//根据状态修改footer显示或隐藏
                mFooterHolder.setData(getHasMoreState());
            }
            return;
        }
        super.onBindViewHolder(holder, position);
    }

    /**控制footer的显示或隐藏*/
    private boolean hasMore;

    public void setHasMoreState(boolean hasMore){
        this.hasMore = hasMore;
    }

    public boolean getHasMoreState(){
        return this.hasMore;
    }

    public FooterHolder getMoreHolder() {
        if(mFooterHolder == null){
            mFooterHolder = new FooterHolder(View.inflate(mContext,R.layout.item_comment_foot_view,null));
        }
        return mFooterHolder;
    }

    @Override
    protected abstract void convert(LViewHolder holder, T t);
}
